package com.ptit.giaothong.tracnghiem;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

import com.ptit.giaothong.DelayActivity;
import com.ptit.giaothong.MainActivity;
import com.ptit.giaothong.R;
import com.ptit.giaothong.database.DatabaseHelper;

public class PlayGame implements OnClickListener {
	private Activity activity;

	public  long total_time;
	public static CountDownTimer countDownTimer;
	public long timer;

	private  TextView date;
	private TranslateAnimation moveControl;

	private int stt = 0;
	private int isCheckquestion = -1;
	private int countPoint = 0;
	private int TRUE_QUESION = 0;
	private int MOVE_LEFT = 1;
	private int MOVE_RIGHT = 2;

	private boolean checkSound = false;
	private boolean isCheckbg = false;

	private final int REFRESH_RATE = 1000;
	private final int TOTAL_TIMER = 20;

	private Button btn_a, btn_b, btn_c, btn_d;
	private TextView txtquestion;
	private ArrayList<Button> listButtons;
	private ArrayList<Itemgame> listQuestion;
	private MediaPlayer mp;

	private String[] mapResult = { "a", "b", "c", "d" };
	private String valueResult;

	private CountDownTimer timerImg;
	private int[] soundCheck = { R.raw.ans_a, R.raw.ans_b, R.raw.ans_c,
			R.raw.ans_d };
	private int[] soundTrue = { R.raw.true_a, R.raw.true_b, R.raw.true_c,
			R.raw.true_d };
	private int[] soundStt = { R.raw.ques01, R.raw.ques02, R.raw.ques03,
			R.raw.ques04, R.raw.ques05, R.raw.ques06, R.raw.ques07,
			R.raw.ques08, R.raw.ques09, R.raw.ques10, R.raw.ques11,
			R.raw.ques12, R.raw.ques13, R.raw.ques14, R.raw.ques15 };
	private Random random = new Random();

	public PlayGame(Activity activity) {
		this.activity = activity;
	}

	public void init(int keypage) {

		MainActivity.keypage = keypage;
		activity.setContentView(R.layout.playgame);

		Cursor c = DelayActivity.myDbHelper.query(DatabaseHelper.TB_QUESTION,
				null, null, null, null, null, null);
		int keyOption = 1 + random.nextInt(6);
		String sql = "";
		switch (keyOption) {
		case 1:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY cauhoi DESC; ";
			break;
		case 2:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY a DESC; ";
			break;
		case 3:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY b DESC; ";
			break;
		case 4:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY c DESC; ";
			break;
		case 5:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY d DESC; ";
			break;
		default:
			sql = "select * from " + DatabaseHelper.TB_QUESTION
					+ " ORDER BY dapan DESC; ";
			break;
		}
		c = DelayActivity.myDbHelper.rawQuery(sql);
		int nID = 0;
		listQuestion = new ArrayList<Itemgame>();
		if (c.moveToFirst()) {
			do {
				Itemgame item = new Itemgame();
				item.setQuestion("Câu hỏi " + (nID + 1) + " :" + c.getString(1));
				item.setResponeA("A. " + c.getString(2));
				item.setResponeB("B. " + c.getString(3));
				item.setResponeC("C. " + c.getString(4));
				item.setResponeD("D. " + c.getString(5));
				item.setIndex(c.getString(6));
				listQuestion.add(item);
				if (nID == 14) {
					break;
				}
				nID++;
			} while (c.moveToNext());
		}
		getView();
		playActionGame(TOTAL_TIMER);
		isReply();
	}

	public void startMoveAnimation(boolean isCheck) {
		for (int i = 0; i < listButtons.size(); i++) {
			if (!isCheck)
				listButtons.get(i).clearAnimation();
			if (isCheck) {
				if (i % 2 == 0) {
					move(MOVE_RIGHT);
				} else {
					move(MOVE_LEFT);
				}
				listButtons.get(i).startAnimation(moveControl);
			}
		}

	}

	public void move(int keyoption) {
		if (keyoption == MOVE_RIGHT) {
			moveControl = new TranslateAnimation(-400, 0, 0, 0);
			moveControl.setDuration(1000);
			moveControl.setFillAfter(true);
		}

		if (keyoption == MOVE_LEFT) {
			moveControl = new TranslateAnimation(400, 0, 0, 0);
			moveControl.setDuration(1000);
			moveControl.setFillAfter(true);
		}
	}

	public void playActionGame(int time) {
		mapQuestion(stt);
		resetSystem();
		checkSound = true;
		countDownTimer = new CountDownTimer(time * 1000, REFRESH_RATE) {
			public void onTick(long millisUntilFinished) {

				total_time = (millisUntilFinished / 1000);
				int sdHoure = (int) (total_time % 3600);
				int sec = sdHoure % 60;
				date.setText("00:00:" + String.valueOf(sec));
				Log.d("sec",sec+"");
				if (total_time == 19) {
					startMoveAnimation(true);
				}
				if (total_time == 18) {
					startMoveAnimation(false);
				}
				if (total_time < 10) {
					date.setTextColor(Color.RED);
				} else {
					date.setTextColor(Color.WHITE);
				}
				if (isCheckquestion != -1 && checkSound) {
					mp = MediaPlayer.create(activity,
							soundCheck[isCheckquestion]);
					mp.start();
					checkSound = false;
				}
				
				if (total_time==4&& listQuestion.get(stt).getIndex()
						.equalsIgnoreCase(valueResult)
						&& isCheckquestion != -1) {
					mp = MediaPlayer.create(activity,
							soundTrue[isCheckquestion]);
					mp.start();
					countPoint += 20;
					TRUE_QUESION++;
				}
			}

			public void onFinish() {
				stt++;
				if (stt >= listQuestion.size()) {
					mp = MediaPlayer.create(activity, R.raw.lose);
					mp.start();
					stt = 0;
					countDownTimer.cancel();
					Result result = new Result(activity);
					result.Init(countPoint, TRUE_QUESION);
				} else {
					playActionGame(TOTAL_TIMER);
				}
			}
		}.start();
	}

	public void sleepTimer(int s) {
		try {
			Thread.sleep(s * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void resetSystem() {
		isCheckquestion = -1;
		for (int i = 0; i < listButtons.size(); i++) {
			listButtons.get(i).setBackgroundResource(R.drawable.select_click);
		}
	}

	public void setBackgroundChecked(int time) {
		timerImg = new CountDownTimer(time * 1000, 50) {
			public void onTick(long millisUntilFinished) {
				if (isCheckquestion != -1) {
					if (isCheckbg) {
						listButtons.get(isCheckquestion).setBackgroundResource(
								R.drawable.select_click);
					} else {
						listButtons.get(isCheckquestion).setBackgroundResource(
								R.drawable.select);
					}
					isCheckbg = !isCheckbg;
				}
			}

			@Override
			public void onFinish() {
				timerImg.cancel();
			}

		}.start();
	}

	public void getView() {
		btn_a = (Button) activity.findViewById(R.id.btn_a);
		btn_b = (Button) activity.findViewById(R.id.btn_b);
		btn_c = (Button) activity.findViewById(R.id.btn_c);
		btn_d = (Button) activity.findViewById(R.id.btn_d);
		listButtons = new ArrayList<Button>();
		listButtons.add(btn_a);
		listButtons.add(btn_b);
		listButtons.add(btn_c);
		listButtons.add(btn_d);
		txtquestion = (TextView) activity.findViewById(R.id.txtquestion);
		date = (TextView) activity.findViewById(R.id.txdate);
	}

	public void mapQuestion(int key) {
		mp = MediaPlayer.create(activity, soundStt[key]);
		mp.start();
		for (int i = 0; i < listQuestion.size(); i++) {
			if (i == key) {
				txtquestion.setText(listQuestion.get(key).getQuestion());
				listButtons.get(0).setText(listQuestion.get(i).getResponeA());
				listButtons.get(1).setText(listQuestion.get(i).getResponeB());
				listButtons.get(2).setText(listQuestion.get(i).getResponeC());
				listButtons.get(3).setText(listQuestion.get(i).getResponeD());
			}
		}
	}

	public void isReply() {
		for (int i = 0; i < listButtons.size(); i++) {
			listButtons.get(i).setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		if (isCheckquestion != -1) {
			listButtons.get(isCheckquestion).setBackgroundResource(
					R.drawable.select);
		} else {
			for (int i = 0; i < listButtons.size(); i++) {
				if (v == listButtons.get(i)) {
					isCheckquestion = i;
					valueResult = mapResult[i];
					listButtons.get(i).setBackgroundResource(R.drawable.select);
				} else {
					listButtons.get(i).setBackgroundResource(
							R.drawable.select_click);
				}

			}
		}
	}
}
